/**
 * @title MatchListAdapter.java
 * @package com.esun.cheetah.view.match.matchlist.adapter
 * @description 
 * @author liugan
 * @date 2012-7-31
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.match.matchlist.adapter;

import java.util.LinkedHashMap;
import java.util.Map;

import com.esun.cheetah.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;

/**
 * @title SeparatedMatchListAdapter
 * @description 按天分开的比赛列表数据适配器
 * @author liugan
 */
public class SeparatedMatchListAdapter extends BaseAdapter{
	
	/**
	 * 按日期划分的比赛列表块
	 */
	private final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
	
	/**
	 * 标题
	 */
	private final ArrayAdapter<String> headers;
	private static final  int TYPE_SECTION_HEADER = 0;
	private static final int TYPE_SECTION_ITEM = 1;

	public SeparatedMatchListAdapter(Context context) {
		headers = new ArrayAdapter<String>(context,R.layout.match_list_header, R.id.tv_section_title);
	}

	/**
	 * @title addSection
	 * @description 添加一组记录
	 * @param section 日期标题
	 * @param adapter 当天的比赛信息
	 */
	public void addSection(String section, Adapter adapter) {
		this.headers.add(section);
		this.sections.put(section, adapter);
	}

	public Object getItem(int position) {
		for(Object section : this.sections.keySet()) {
			Adapter adapter = sections.get(section);
			int size = adapter.getCount() + 1;

			// check if position inside this section
			if(position == 0) return section;
			if(position < size) return adapter.getItem(position - 1);

			// otherwise jump into next section
			position -= size;
		}
		return null;
	}

	public int getCount() {
		// total together all sections, plus one for each section header
		int total = 0;
		for(Adapter adapter : this.sections.values())
			total += adapter.getCount() + 1;
		return total;
	}

	//仅有两种View类型，即比赛项和日期标题
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		for(Object section : this.sections.keySet()) {
			Adapter adapter = sections.get(section);
			int size = adapter.getCount() + 1;

			// check if position inside this section
			if(position == 0) return TYPE_SECTION_HEADER;
			if(position < size) return TYPE_SECTION_ITEM;

			// otherwise jump into next section
			position -= size;
		}
		return -1;
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	public boolean isEnabled(int position) {
		return (getItemViewType(position) != TYPE_SECTION_HEADER);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int sectionnum = 0;
		for(Object section : this.sections.keySet()) {
			Adapter adapter = sections.get(section);
			int size = adapter.getCount() + 1;

			// check if position inside this section
			if(position == 0) return headers.getView(sectionnum, convertView, parent);
			if(position < size) return adapter.getView(position - 1, convertView, parent);

			// otherwise jump into next section
			position -= size;
			sectionnum++;
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void clear(){
		headers.clear();
		sections.clear();
	}
}
